package net.flydy.ywxcx.wxcloudrun.config;

import lombok.extern.slf4j.Slf4j;
import net.flydy.ywxcx.wxcloudrun.common.StatusEnum;
import net.flydy.ywxcx.wxcloudrun.vo.BaseResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.ContentCachingRequestWrapper;

import javax.servlet.ServletRequest;
import java.nio.charset.Charset;

/**
 * @author flydy
 */
@Slf4j
@Component
//@ControllerAdvice(basePackages = {"net.signal.signal_server.controller"})
@ControllerAdvice
public class GlobalExceptionHandler {

//    @ExceptionHandler(value = JsonParseException.class)
//    public @ResponseBody BaseResponse exceptionHandler(JsonParseException e, ServletRequest request) {
//        if (request != null && request instanceof ContentCachingRequestWrapper) {
//            ContentCachingRequestWrapper wrapper = (ContentCachingRequestWrapper) request;
//            log.error("BAD_REQUEST_BODY:{}", StringUtils.toEncodedString(wrapper.getContentAsByteArray(),
//                    Charset.forName(wrapper.getCharacterEncoding())));
//        }
//        BaseResponse baseResponse = new BaseResponse();
//        baseResponse.setCode(StatusEnum.VALIDATION_FAIL.getCode());
//        baseResponse.setMessage("调用接口异常，解析请求体JSON格式错误");
//        return baseResponse;
//    }
    @ResponseBody
    @ExceptionHandler(value = Throwable.class)
    public BaseResponse exceptionHandler(Throwable e, ServletRequest request) {
        log.error("exceptionHandler", e);
        if (request != null && request instanceof ContentCachingRequestWrapper) {
            ContentCachingRequestWrapper wrapper = (ContentCachingRequestWrapper) request;
            log.error("BAD_REQUEST_BODY:{}", StringUtils.toEncodedString(wrapper.getContentAsByteArray(),
                    Charset.forName(wrapper.getCharacterEncoding())));
        }
        BaseResponse baseResponse = new BaseResponse();
        baseResponse.setCode(StatusEnum.VALIDATION_FAIL.getCode());
        baseResponse.setMessage("调用接口异常");
        return baseResponse;
    }
//
//    @ResponseBody
//    @ExceptionHandler(value = NullPointerException.class)
//    public ResponseEntity<Object> nullExceptionHandler(NullPointerException e) {
//        log.info("-------->nullExceptionHandler");
//        return buildResponse(e.getMessage());
//    }
//
//
//    @ResponseBody
//    @ExceptionHandler(value = IllegalArgumentException.class)
//    public ResponseEntity<Object> illegalArgumentExceptionHandler(IllegalArgumentException e) {
//        return buildResponse(e.getMessage());
//    }
//
//
//    @Override
//    protected ResponseEntity<Object> handleMissingServletRequestParameter(MissingServletRequestParameterException ex,
//                                                                          HttpHeaders headers, HttpStatus status,
//                                                                          WebRequest request) {
//        return new ResponseEntity<Object>("ParamMissing:" + ex.getMessage(), HttpStatus.BAD_REQUEST);
//    }
//
//    @Override
//    protected ResponseEntity<Object> handleTypeMismatch(TypeMismatchException ex, HttpHeaders headers,
//                                                        HttpStatus status, WebRequest request) {
//        return new ResponseEntity<Object>("TypeMissMatch:" + ex.getMessage(), HttpStatus.BAD_REQUEST);
//    }
//
//    private ResponseEntity<Object> buildResponse(String message) {
//        Map<String, Object> errorAttributes = new LinkedHashMap(3);
//        errorAttributes.put("description", message);
//        errorAttributes.put("error_code", "1900");
//        errorAttributes.put("type", "tt");
//        return new ResponseEntity<>(errorAttributes, BAD_REQUEST);
//    }


}
